package com.meiqiongproperty.yiguanjia.fragment.home.health;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.activity.GenericFragmentActivity;
import com.meiqiongproperty.yiguanjia.databinding.HealthyShopLayoutBinding;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.OnItemClickListener;
import com.meiqiongproperty.yiguanjia.fragment.home.adapter.yiHealthyMedicalAdapter.HealthyShopListAdapter;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.IHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendFreshFruitList;
import com.meiqiongproperty.yiguanjia.httpbean.healthy.HealthyShopListBean;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * author: Administrator
 * date: 2017/5/21.
 * package:com.meiqiongproperty.yiguanjia.fragment.home.health
 */

public class HealthyShopFragment extends BaseFragment {
    HealthyShopLayoutBinding binding;
    HealthyShopListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater, R.layout.healthy_shop_layout,container,false);
        binding.navigation.setTitle("怡健康");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });

        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerHealthyShopList.setLayoutManager(manager);

        getHealthShopList();
        return binding.getRoot();
    }


    /**
     * 保健商城
     */
    public void getHealthShopList(){
        SendFreshFruitList bean = new SendFreshFruitList();
        bean.setToken(Utils.getToken(getActivity()));
        bean.setFirstCate("2");
        ApiService.getHealthyShopList(getActivity(), new IHttpCallback<HealthyShopListBean>() {
            @Override
            public void onNext(final HealthyShopListBean healthyShopListBean) {
                adapter=new HealthyShopListAdapter(getActivity(),healthyShopListBean.getShoplist());
                binding.recyclerHealthyShopList.setAdapter(adapter);
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        Bundle bundle=new Bundle();
                        bundle.putString("productId",healthyShopListBean.getShoplist().get(position).getProductId());
                        GenericFragmentActivity.start(getActivity(),HealthyShopDetailFragment.class,bundle);
                    }
                });
            }

            @Override
            public void onError(int code, String message) {

            }
        }, Utils.getRequestBody(bean));
    }

    @Override
    public void onResume() {
        super.onResume();
        getHealthShopList();
    }
}
